﻿using System;
using System.Text;

namespace SharpOSCAR.Base
{
    /// <summary>
    /// Base string class
    /// </summary>
    public class String : ProtocolData, IString
    {
        private string _Value;
        /// <summary>
        /// Gets/sets the underlying string value
        /// </summary>
        public string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        /// <summary>
        /// Initializes the String instance with the specified value
        /// </summary>
        /// <param name="value">Value of the String</param>
        public String(string value)
        {
            _Value = value;
        }

        /// <summary>
        /// Returns a byte array representing the string
        /// </summary>
        /// <returns>Returns a byte array representing the string</returns>
        public override byte[] ToByteArray()
        {
            // Convert the string into a UTF8 byte array
            return Encoding.UTF8.GetBytes(_Value);
        }

        /// <summary>
        /// Returns a byte array representing the string in "compressed" format
        /// </summary>
        /// <returns>Returns a byte array representing the string in "compressed" format</returns>
        public byte[] ToByteArrayCompressed()
        {
            // Convert the string to lowercase and remove all spaces
            string compressed = _Value.ToLower().Replace(" ", "");
            // Convert the compressed string into a UTF8 byte array
            return Encoding.UTF8.GetBytes(compressed);
        }
    }
}
